<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        prop="schemaName"
        header-align="center"
        align="center"
        label="数据库名称">
      </el-table-column>
      <el-table-column
        prop="characterName"
        header-align="center"
        align="center"
        label="编码格式">
      </el-table-column>
      <el-table-column
        prop="collationName"
        header-align="center"
        align="center"
        label="校对规则">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="selectTablePage(scope.row.schemaName)">部分生成</el-button>
          <el-button type="text" size="small" @click="allGeneratorCode(scope.row.schemaName)">整库生成</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <table-page v-if="tableVisible" ref="tablePage" @refreshDataList="getDataList"></table-page>
  </div>
</template>
<script>
  import tablePage from './generator-table'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        tableVisible: false
      }
    },
    components: {
      tablePage
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/generator/selectDataBasePage'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
    // 查询对应数据库表
      selectTablePage (dataBaseName) {
        this.tableVisible = true
        this.$nextTick(() => {
          this.$refs.tablePage.init(dataBaseName)
        })
      },
      allGeneratorCode (schemaName) {
        this.$http({
          url: this.$http.adornUrl('/generator/allGenerateCode'),
          method: 'post',
          responseType: 'blob',
          params: this.$http.adornParams({
            'dataBaseName': schemaName
          })
        }).then(response => {
          this.download(response.data)
        }).catch((error) => {
          console.log(error)
          this.$message({
            message: '生成失败',
            type: 'error'
          })
        })
      },
      // 下载文件
      download (data) {
        console.log(data)
        if (!data) {
          return
        }
        let url = window.URL.createObjectURL(new Blob([data]))
        let link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', '代码.zip')
        document.body.appendChild(link)
        link.click()
      }
    }
  }
</script>
